package com.note.simpleshop.module.goods.controller;
import com.note.simpleshop.constant.ErrorCode;
import com.note.simpleshop.entity.TbGoods;
import com.note.simpleshop.module.goods.exception.GoodsException;
import com.note.simpleshop.module.goods.service.GoodsService;
import com.note.simpleshop.module.goods.vo.GoodsDetailVo;
import com.note.simpleshop.vo.Msg;
import com.note.simpleshop.vo.PageBean;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("goods/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @ApiOperation(value = "list", notes = "商品展示列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "pageNumber", required = false, paramType = "query"),
            @ApiImplicitParam(name = "classifyId", value = "classifyId", required = false, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "name", required = false, paramType = "query")

    })
    @PostMapping(value = "search")
    public Msg search( Integer pageNumber, String classifyId, String name) {
        Msg msg=new Msg();
        try {
            if (pageNumber == null || pageNumber < 1) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("pageNumber不能空且大于等于1");
                return msg;
            } else
            {
                PageBean<TbGoods> page=goodsService.findByPage(pageNumber,classifyId,name);
                msg.setErrorCode(ErrorCode.ERROR_0000);
                msg.setData(page);
                return msg;
            }
        }
        catch (Exception e)
        {
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @ApiOperation(value = "detail", notes = "商品详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "goodsId", required = false, paramType = "query")

    })
    @PostMapping(value = "detail")
    public Msg detail(String goodsId)
    {
        Msg msg=new Msg();
        try
        {
            if(StringUtils.isBlank(goodsId))
            {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("goodsId不合法");
            }
            GoodsDetailVo goodsDetailVo=goodsService.findDetailOne(goodsId);
            msg.setErrorCode(ErrorCode.ERROR_0000);
            msg.setData(goodsDetailVo);
            msg.setMsg("ok");
        }
        catch (GoodsException e)
        {
            msg.setErrorCode(ErrorCode.ERROR_1000000);
            msg.setMsg(e.getMessage());
        }
        return msg;
    }
}
